
#include "dev_dwled.h"
#include "page_base.h"
#include "img_common.h"
#include "pt_dev.h"
#include "pt_ipc.h"
#include "pt_fs.h"
#include "sys_font.h"

static  char * icon_power[2] =
{
    "Power3_Default.png",
    "Power3_Selected.png",
};

static  char * icon_led_onoff[2] =
{
    "Lamplight_48_Disabled.png",
    "Lamplight_48_Selected.png",
};

static  char * icon_led_w[2] =
{
    "light_ic_colortem_unable.png",
    "light_ic_colortem.png",
};


static int dev_dwled_onoff(ui_dev_dwled_t* dwled)
{
    dwled_http_req_t http_req;    
    memset(&http_req,0,sizeof(dwled_http_req_t));

    char body[128] = {0};
    memset(body,0,sizeof(body));

    if(dwled->data.onoff){
        snprintf(body, 128, "\"light.power\":%s","true");
    } else {
        snprintf(body, 128, "\"light.power\":%s","false");
    }

    snprintf(http_req.devid,sizeof(http_req.devid),"%s",dwled->data.devid);
    snprintf(http_req.body, sizeof(http_req.body), "%s",body);

    debug_d("devid=%s\n",http_req.devid);
    debug_d("body =%s\n",http_req.body);

    pt_ipc_send_msg(IPC_MSG_DWLED_ONOFF,(void*)&http_req,sizeof(dwled_http_req_t));

    return 0;
}

static int dev_dwled_set_brightness(ui_dev_dwled_t* dwled)
{
    dwled_http_req_t http_req;   
    memset(&http_req,0,sizeof(dwled_http_req_t)); 

    char body[128] = {0};
    memset(body,0,sizeof(body));

    if(dwled->data.brightness > 100){
        return 0;
    }

    if(dwled->data.brightness < 1){
        return 0;
    }

    snprintf(body, 128, "\"light.brightness\":%d",dwled->data.brightness);

    snprintf(http_req.devid,sizeof(http_req.devid),"%s",dwled->data.devid);
    snprintf(http_req.body, sizeof(http_req.body), "%s",body);

    debug_d("devid=%s\n",http_req.devid);
    debug_d("body =%s\n",http_req.body);

    pt_ipc_send_msg(IPC_MSG_DWLED_ONOFF,(void*)&http_req,sizeof(dwled_http_req_t));

    return 0;
}

static int dev_dwled_set_colortemp(ui_dev_dwled_t* dwled)
{
    dwled_http_req_t http_req;    
    memset(&http_req,0,sizeof(dwled_http_req_t));

    char body[128] = {0};
    memset(body,0,sizeof(body));

    if(dwled->data.color_temp > 5000){
        return 0;
    }

    if(dwled->data.color_temp < 1800){
        return 0;
    }

    snprintf(body, 128, "\"light.colorTemperature\":%d",dwled->data.color_temp);

    snprintf(http_req.devid,sizeof(http_req.devid),"%s",dwled->data.devid);
    snprintf(http_req.body, sizeof(http_req.body), "%s",body);

    pt_ipc_send_msg(IPC_MSG_DWLED_ONOFF,(void*)&http_req,sizeof(dwled_http_req_t));

    return 0;
}

static int dev_dwled_power_clicked(ui_dev_dwled_t* dwled)
{
    if(!dwled->ui.btn_img)
        return -1;

    if(!dwled->ui.bar_img)
        return -1;

    dwled->data.onoff = !dwled->data.onoff;

    lv_img_set_src(dwled->ui.btn_img,pt_fs_get(icon_power[dwled->data.onoff]));
    lv_img_set_src(dwled->ui.bar_img,pt_fs_get(icon_led_onoff[dwled->data.onoff]));
	lv_img_set_src(dwled->ui.bar_w_img,pt_fs_get(icon_led_w[dwled->data.onoff]));
    
    // if(dwled->data.onoff)
    // {
    //     lv_obj_set_style_bg_color(dwled->ui.bar, lv_color_make(0x4e, 0xb2, 0xff), LV_PART_INDICATOR);
    //     lv_obj_set_style_bg_grad_color(dwled->ui.bar,lv_color_make(0x26, 0x7a, 0xff), LV_PART_INDICATOR);
    //     lv_obj_set_style_bg_color(dwled->ui.bar_w, lv_color_make(0xff, 0x82, 0x25), LV_PART_INDICATOR);
    //     lv_obj_set_style_bg_grad_color(dwled->ui.bar_w,lv_color_make(0xff, 0xaa, 0x10), LV_PART_INDICATOR);
    // }
    // else
    // {
    //     lv_obj_set_style_bg_color(dwled->ui.bar, lv_color_make(0x52, 0x5a, 0x6a), LV_PART_INDICATOR);
    //     lv_obj_set_style_bg_grad_color(dwled->ui.bar,lv_color_make(0x52, 0x5a, 0x6a), LV_PART_INDICATOR);
    //     lv_obj_set_style_bg_color(dwled->ui.bar_w, lv_color_make(0x52, 0x5a, 0x6a), LV_PART_INDICATOR);
    //     lv_obj_set_style_bg_grad_color(dwled->ui.bar_w,lv_color_make(0x52, 0x5a, 0x6a), LV_PART_INDICATOR);
    // }

    dev_dwled_onoff(dwled);

    return 0;
}

static int dev_dwled_power_pressed(ui_dev_dwled_t* dwled)
{


    return 0;
}
static int dev_dwled_power_released(ui_dev_dwled_t* dwled)
{


    return 0;
}
static int dev_dwled_power_pressing(ui_dev_dwled_t* dwled)
{


    return 0;
}

static void dev_dwled_power_event_callback(lv_event_t* event)
{
    ui_dev_dwled_t* dwled = NULL;
    dwled = (ui_dev_dwled_t*)lv_event_get_user_data(event);

    switch(event->code){
    case LV_EVENT_CLICKED:
    {
        debug_d("clicked\n");
        dev_dwled_power_clicked(dwled);
        break;
    }
    case LV_EVENT_RELEASED:
    {
        debug_d("relea\n");
        dev_dwled_power_released(dwled);
        break;
    }
    case LV_EVENT_PRESSED:
    {
        debug_d("pressed\n");
        dev_dwled_power_pressed(dwled);
        break;
    }
    case LV_EVENT_PRESSING:
    {
        debug_d("pressing\n");
        dev_dwled_power_pressing(dwled);
        break;
    }
    default:break;
    }
}

static void dev_dwled_bar_event_callback(lv_event_t* event)
{
    lv_area_t   bar_area;
    lv_point_t  lv_point;
    lv_indev_t* indev = NULL;

    ui_dev_dwled_t* dwled = NULL;
    dwled = (ui_dev_dwled_t*)lv_event_get_user_data(event);

    int w = 0;
    int h = 0;
    int t_h = 0;
    int vol = 0;

    char txt_buff[128];

    if(!dwled->data.onoff)
    {
        return;
    }

    indev = lv_indev_get_act();
    if(!indev){
        return ;
    }

    if(!dwled->ui.bar)
        return ;
    if(!dwled->ui.bar_txt)
        return ;

    lv_indev_get_point(indev, &lv_point);
    lv_obj_get_coords(dwled->ui.bar, &bar_area);

    w = bar_area.x2 - bar_area.x1 + 1;
    h = bar_area.y2 - bar_area.y1 + 1;

    t_h = bar_area.y2 - lv_point.y + 1;

    if(t_h < 0)
        t_h = 0;
    if(t_h > h)
        t_h = h;

    switch(event->code){
    case LV_EVENT_PRESSED:
    {
        root_gesture_disable();
        root_page_disable_scroll();
        break;
    }
    case LV_EVENT_RELEASED:
    {
        root_gesture_enable();
        root_page_enable_scroll();
        break;
    }
    case LV_EVENT_CLICKED:
    case LV_EVENT_PRESSING:
    {
        vol = t_h*dwled->data.max_bright/h;
        
        dwled->data.brightness = vol;

        lv_bar_set_value(dwled->ui.bar,vol, LV_ANIM_OFF);

        memset(txt_buff,0,sizeof(txt_buff));
        snprintf(txt_buff,128,"%d%%",vol);
        lv_label_set_text(dwled->ui.bar_txt,txt_buff);

        debug_d("dwval=%d\n",vol);

        if(event->code == LV_EVENT_CLICKED)
        {
            dev_dwled_set_brightness(dwled);
        }
        break;
    }
    default:break;
    }
}

static void dev_dwled_bar_w_event_callback(lv_event_t* event)
{
    lv_area_t   bar_area;
    lv_point_t  lv_point;
    lv_indev_t* indev = NULL;
    ui_dev_dwled_t* dwled = NULL;
    dwled = (ui_dev_dwled_t*)lv_event_get_user_data(event);

    int w = 0;
    int h = 0;
    int t_h = 0;
    int vol = 0;

    char txt_buff[128];

    if(!dwled->data.onoff)
    {
        return;
    }

    indev = lv_indev_get_act();
    if(!indev){
        return ;
    }
    if(!dwled->ui.bar_w)
        return ;  
    if(!dwled->ui.bar_w_txt)
        return ;  
          
    lv_indev_get_point(indev, &lv_point);
    lv_obj_get_coords(dwled->ui.bar_w, &bar_area);

    w = bar_area.x2 - bar_area.x1 + 1;
    h = bar_area.y2 - bar_area.y1 + 1;

    t_h = bar_area.y2 - lv_point.y + 1;

    if(t_h < 0)
        t_h = 0;
    if(t_h > h)
        t_h = h;

    switch(event->code){
    case LV_EVENT_PRESSED:
    {
        root_gesture_disable();
        root_page_disable_scroll();
        break;
    }
    case LV_EVENT_RELEASED:
    {
        root_gesture_enable();
        root_page_enable_scroll();
        break;
    }
    case LV_EVENT_CLICKED:
    case LV_EVENT_PRESSING:
    {
        int max = dwled->data.max_colortemp;
        int min = dwled->data.min_colortemp;

        vol = t_h*(max-min)/h + min;
        lv_bar_set_value(dwled->ui.bar_w,vol, LV_ANIM_OFF);

        memset(txt_buff,0,sizeof(txt_buff));
        snprintf(txt_buff,128,"%dK",vol);
        lv_label_set_text(dwled->ui.bar_w_txt,txt_buff);

        debug_d("dwval=%d\n",vol);

        dwled->data.color_temp = vol;

        if(event->code == LV_EVENT_CLICKED)
        {
            dev_dwled_set_colortemp(dwled);                
        }
        break;
    }
    default:break;
    }
}

int ui_dev_dwled_status_update(ui_dev_dwled_t* dwled)
{
    static int onoff = 0;
    static int brightness = 0;
    static int color_temp = 0;

    char txt_buff[128];

    if(!dwled){
        return -1;
    }

    if((onoff      != dwled->data.onoff)||
       (brightness != dwled->data.brightness)||
       (color_temp != dwled->data.color_temp))
    {
        onoff      = dwled->data.onoff;
        brightness = dwled->data.brightness;
        color_temp = dwled->data.color_temp;
    }
    else{
        return -1;
    }

    lv_img_set_src(dwled->ui.btn_img,pt_fs_get(icon_power[dwled->data.onoff]));
	lv_bar_set_value(dwled->ui.bar,dwled->data.brightness, LV_ANIM_OFF);
	lv_img_set_src(dwled->ui.bar_img,pt_fs_get(icon_led_onoff[dwled->data.onoff]));

	memset(txt_buff,0,sizeof(txt_buff));
    snprintf(txt_buff,sizeof(txt_buff),"%d%%",dwled->data.brightness);
    lv_label_set_text( dwled->ui.bar_txt,txt_buff);

	lv_bar_set_value(dwled->ui.bar_w,dwled->data.color_temp, LV_ANIM_OFF);
	lv_img_set_src(dwled->ui.bar_w_img,pt_fs_get(icon_led_w[dwled->data.onoff]));

    memset(txt_buff,0,sizeof(txt_buff));
    snprintf(txt_buff,sizeof(txt_buff),"%dK",dwled->data.color_temp);
    lv_label_set_text(dwled->ui.bar_w_txt,txt_buff);

	// if(dwled->data.onoff)
    // {
    //     lv_obj_set_style_bg_color(dwled->ui.bar, lv_color_make(0x4e, 0xb2, 0xff), LV_PART_INDICATOR);
    //     lv_obj_set_style_bg_grad_color(dwled->ui.bar,lv_color_make(0x26, 0x7a, 0xff), LV_PART_INDICATOR);
    //     lv_obj_set_style_bg_color(dwled->ui.bar_w, lv_color_make(0xff, 0x82, 0x25), LV_PART_INDICATOR);
    //     lv_obj_set_style_bg_grad_color(dwled->ui.bar_w,lv_color_make(0xff, 0xaa, 0x10), LV_PART_INDICATOR);
    // }
    // else
    // {
    //     lv_obj_set_style_bg_color(dwled->ui.bar, lv_color_make(0x52, 0x5a, 0x6a), LV_PART_INDICATOR);
    //     lv_obj_set_style_bg_grad_color(dwled->ui.bar,lv_color_make(0x52, 0x5a, 0x6a), LV_PART_INDICATOR);
    //     lv_obj_set_style_bg_color(dwled->ui.bar_w, lv_color_make(0x52, 0x5a, 0x6a), LV_PART_INDICATOR);
    //     lv_obj_set_style_bg_grad_color(dwled->ui.bar_w,lv_color_make(0x52, 0x5a, 0x6a), LV_PART_INDICATOR);
    // }

    return 0;
}

static void dev_dwled_update_callback(struct _lv_timer_t *t)
{
    ui_dev_dwled_t* dwled     = NULL;
    dwled = (ui_dev_dwled_t*)t->user_data;
    if(!dwled){
        return ;
    }

    ui_dev_dwled_status_update(dwled);

    return ;
}

int ui_dev_dwled_hide(ui_dev_dwled_t* dwled)
{
    if(!dwled){
        return -1;
    }

    lv_obj_add_flag(dwled->ui.bg,LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(dwled->ui.bg_img,LV_OBJ_FLAG_HIDDEN);

    lv_obj_add_flag(dwled->ui.btn,LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(dwled->ui.btn_img,LV_OBJ_FLAG_HIDDEN);

    lv_obj_add_flag(dwled->ui.bar,LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(dwled->ui.bar_img,LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(dwled->ui.bar_txt,LV_OBJ_FLAG_HIDDEN);

    lv_obj_add_flag(dwled->ui.bar_w,LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(dwled->ui.bar_w_img,LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(dwled->ui.bar_w_txt,LV_OBJ_FLAG_HIDDEN);

    lv_timer_pause(dwled->ui.time);

    return 0;
}

int ui_dev_dwled_show(ui_dev_dwled_t* dwled)
{
    if(!dwled){
        return -1;
    }

    lv_obj_clear_flag(dwled->ui.bg,LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(dwled->ui.bg_img,LV_OBJ_FLAG_HIDDEN);

    lv_obj_clear_flag(dwled->ui.btn,LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(dwled->ui.btn_img,LV_OBJ_FLAG_HIDDEN);

    lv_obj_clear_flag(dwled->ui.bar,LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(dwled->ui.bar_img,LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(dwled->ui.bar_txt,LV_OBJ_FLAG_HIDDEN);

    lv_obj_clear_flag(dwled->ui.bar_w,LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(dwled->ui.bar_w_img,LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(dwled->ui.bar_w_txt,LV_OBJ_FLAG_HIDDEN);
    
    lv_timer_resume(dwled->ui.time);

    return 0;
}


int ui_dev_dwled_create(lv_obj_t* parent,void* ui_data)
{
    char txt_buff[256];
    lv_coord_t pw = 0;
    lv_coord_t ph = 0;

    ui_dev_dwled_t* dwled     = (ui_dev_dwled_t*)ui_data;
    lv_obj_t      * superview = parent;
    
    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);

    dwled->ui.bg  = lv_obj_create(superview);

    lv_obj_set_style_radius(dwled->ui.bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(dwled->ui.bg, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(dwled->ui.bg, LV_OPA_0, LV_PART_MAIN);
	lv_obj_set_style_border_color(dwled->ui.bg, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
    lv_obj_set_style_pad_all(dwled->ui.bg,0,0);

    #if 1
	lv_obj_set_style_bg_opa(dwled->ui.bg, LV_OPA_0, LV_PART_MAIN);
    #else
	lv_obj_set_style_bg_opa(dwled->ui.bg, LV_OPA_50, LV_PART_MAIN);
	lv_obj_set_style_bg_color(dwled->ui.bg, lv_color_make(0xFF,0,0), LV_PART_MAIN);
    #endif

	lv_obj_set_pos(dwled->ui.bg,0,0);
	lv_obj_set_size(dwled->ui.bg,pw,ph);
    //****************************************************** */
    dwled->ui.bg_img = lv_img_create(superview);
    #if 0
    lv_img_set_src(dwled->ui.bg_img,pt_fs_get("BG_Blue.png"));
    #else
    lv_img_set_src(dwled->ui.bg_img,&BG_Blue);
    #endif
    lv_obj_set_pos(dwled->ui.bg_img,0,0);
    //***************************************************************************
    dwled->ui.btn = lv_obj_create(superview);

    lv_obj_set_pos(dwled->ui.btn,50,128);
    lv_obj_set_size(dwled->ui.btn,96,96);

	lv_obj_set_style_radius(dwled->ui.btn, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(dwled->ui.btn, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(dwled->ui.btn, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(dwled->ui.btn, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(dwled->ui.btn, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(dwled->ui.btn, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(dwled->ui.btn, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(dwled->ui.btn, 0,0);

    #if 1
    lv_obj_set_style_bg_opa(dwled->ui.btn,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(dwled->ui.btn,LV_OPA_100,0);
    lv_obj_set_style_bg_color(dwled->ui.btn,lv_color_make(0xff,0,0),0);
    #endif

    lv_obj_clear_flag(dwled->ui.btn,LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_event_cb(dwled->ui.btn, dev_dwled_power_event_callback, LV_EVENT_ALL,(void*)dwled);
    //*****************************************
    dwled->ui.btn_img = lv_img_create(dwled->ui.btn);
    lv_img_set_src(dwled->ui.btn_img,pt_fs_get(icon_power[dwled->data.onoff]));
    lv_obj_set_pos(dwled->ui.btn_img,0,0);
    //*****************************************
    //bar

	dwled->ui.bar = lv_bar_create(superview);

	lv_obj_set_pos(dwled->ui.bar,226,65);
	lv_obj_set_size(dwled->ui.bar,70,222);

    lv_obj_set_style_radius(dwled->ui.bar, 40, LV_PART_MAIN);
	lv_obj_set_style_border_width(dwled->ui.bar, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(dwled->ui.bar, LV_OPA_0, LV_PART_MAIN);
	lv_obj_set_style_border_color(dwled->ui.bar, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);

	lv_bar_set_range(dwled->ui.bar,dwled->data.min_bright,dwled->data.max_bright);
	lv_bar_set_mode(dwled->ui.bar, LV_BAR_MODE_SYMMETRICAL);

#if 0
	lv_obj_set_style_bg_opa(dwled->ui.bar, LV_OPA_0, LV_PART_MAIN);
#else
	lv_obj_set_style_bg_opa(dwled->ui.bar, LV_OPA_10, LV_PART_MAIN);
	lv_obj_set_style_bg_color(dwled->ui.bar, lv_color_make(0xff, 0xff, 0xff), LV_PART_MAIN);
#endif

	lv_obj_set_style_bg_opa(dwled->ui.bar, LV_OPA_100, LV_PART_INDICATOR);
	lv_obj_set_style_bg_grad_dir(dwled->ui.bar,LV_GRAD_DIR_VER,LV_PART_INDICATOR);

	lv_bar_set_value(dwled->ui.bar,dwled->data.brightness, LV_ANIM_OFF);

    lv_obj_clear_flag(dwled->ui.bar,LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_event_cb(dwled->ui.bar, dev_dwled_bar_event_callback, LV_EVENT_ALL,(void*)dwled);
    //***************************************************************

    dwled->ui.bar_img = lv_img_create(superview);
    lv_img_set_src(dwled->ui.bar_img,pt_fs_get(icon_led_onoff[dwled->data.onoff]));
    lv_obj_set_pos(dwled->ui.bar_img,237,232);
    //***************************************************************
    dwled->ui.bar_txt = lv_label_create(superview);
    lv_obj_set_pos(dwled->ui.bar_txt,237,209);
    lv_obj_set_size(dwled->ui.bar_txt,50,23);
    lv_obj_set_style_text_align(dwled->ui.bar_txt,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(dwled->ui.bar_txt,LV_OPA_80,LV_PART_MAIN);
    lv_obj_set_style_text_color(dwled->ui.bar_txt,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_font(dwled->ui.bar_txt, lv_font_get(18), LV_STATE_DEFAULT);

#if 1
	lv_obj_set_style_bg_opa(dwled->ui.bar_txt, LV_OPA_0, LV_PART_MAIN);
#else
	lv_obj_set_style_bg_opa(dwled->ui.bar_txt, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(dwled->ui.bar_txt, lv_color_make(0xFF, 0, 0), LV_PART_MAIN);
#endif
    memset(txt_buff,0,sizeof(txt_buff));
    snprintf(txt_buff,256,"%d%%",dwled->data.brightness);
    lv_label_set_text( dwled->ui.bar_txt,txt_buff);
    //***************************************************************
    //bar2

	dwled->ui.bar_w = lv_bar_create(superview);

	lv_obj_set_pos(dwled->ui.bar_w,350,65);
	lv_obj_set_size(dwled->ui.bar_w,70,222);

    lv_obj_set_style_radius(dwled->ui.bar_w, 40, LV_PART_MAIN);
	lv_obj_set_style_border_width(dwled->ui.bar_w, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(dwled->ui.bar_w, LV_OPA_0, LV_PART_MAIN);
	lv_obj_set_style_border_color(dwled->ui.bar_w, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);

	lv_bar_set_range(dwled->ui.bar_w,dwled->data.min_colortemp,dwled->data.max_colortemp);
	lv_bar_set_mode(dwled->ui.bar_w, LV_BAR_MODE_SYMMETRICAL);

#if 0
	lv_obj_set_style_bg_opa(dwled->ui.bar_w, LV_OPA_0, LV_PART_MAIN);
#else
	lv_obj_set_style_bg_opa(dwled->ui.bar_w, LV_OPA_10, LV_PART_MAIN);
	lv_obj_set_style_bg_color(dwled->ui.bar_w, lv_color_make(0xff,0xff, 0xff), LV_PART_MAIN);
#endif

	lv_obj_set_style_bg_opa(dwled->ui.bar_w, LV_OPA_100, LV_PART_INDICATOR);
	lv_obj_set_style_bg_grad_dir(dwled->ui.bar_w,LV_GRAD_DIR_VER,LV_PART_INDICATOR);

	lv_bar_set_value(dwled->ui.bar_w,dwled->data.color_temp, LV_ANIM_OFF);
    lv_obj_clear_flag(dwled->ui.bar_w,LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_event_cb(dwled->ui.bar_w, dev_dwled_bar_w_event_callback, LV_EVENT_ALL,(void*)dwled);
    
    
    if(dwled->data.onoff)
    {
        lv_obj_set_style_bg_color(dwled->ui.bar, lv_color_make(0x4e, 0xb2, 0xff), LV_PART_INDICATOR);
        lv_obj_set_style_bg_grad_color(dwled->ui.bar,lv_color_make(0x26, 0x7a, 0xff), LV_PART_INDICATOR);
        lv_obj_set_style_bg_color(dwled->ui.bar_w, lv_color_make(0xff, 0x82, 0x25), LV_PART_INDICATOR);
        lv_obj_set_style_bg_grad_color(dwled->ui.bar_w,lv_color_make(0xff, 0xaa, 0x10), LV_PART_INDICATOR);
    }
    else
    {
        lv_obj_set_style_bg_color(dwled->ui.bar, lv_color_make(0x52, 0x5a, 0x6a), LV_PART_INDICATOR);
        lv_obj_set_style_bg_grad_color(dwled->ui.bar,lv_color_make(0x52, 0x5a, 0x6a), LV_PART_INDICATOR);
        lv_obj_set_style_bg_color(dwled->ui.bar_w, lv_color_make(0x52, 0x5a, 0x6a), LV_PART_INDICATOR);
        lv_obj_set_style_bg_grad_color(dwled->ui.bar_w,lv_color_make(0x52, 0x5a, 0x6a), LV_PART_INDICATOR);
    }
    //***************************************************************

    dwled->ui.bar_w_img = lv_img_create(superview);
    lv_img_set_src(dwled->ui.bar_w_img,pt_fs_get(icon_led_w[dwled->data.onoff]));
    lv_obj_set_pos(dwled->ui.bar_w_img,365,235);
    //***************************************************************
    dwled->ui.bar_w_txt = lv_label_create(superview);
    lv_obj_set_pos(dwled->ui.bar_w_txt,352,209);
    lv_obj_set_size(dwled->ui.bar_w_txt,66,23);
    lv_obj_set_style_text_align(dwled->ui.bar_w_txt,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(dwled->ui.bar_w_txt,LV_OPA_80,LV_PART_MAIN);
    lv_obj_set_style_text_color(dwled->ui.bar_w_txt,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_font(dwled->ui.bar_w_txt, lv_font_get(20), LV_STATE_DEFAULT);

#if 1
	lv_obj_set_style_bg_opa(dwled->ui.bar_w_txt, LV_OPA_0, LV_PART_MAIN);
#else
	lv_obj_set_style_bg_opa(dwled->ui.bar_w_txt, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(dwled->ui.bar_w_txt, lv_color_make(0xFF, 0, 0), LV_PART_MAIN);
#endif

    memset(txt_buff,0,sizeof(txt_buff));
    snprintf(txt_buff,256,"%dK",dwled->data.color_temp);
    lv_label_set_text(dwled->ui.bar_w_txt,txt_buff);
    //***************************************************************

    dwled->ui.time = lv_timer_create(dev_dwled_update_callback,1000,(void*)dwled);

    return 0;
}


int ui_dev_dwled_destroy(void* ui_data)
{
    ui_dev_dwled_t* dwled =(ui_dev_dwled_t*)ui_data;

    if(!dwled){
        return -1;
    }

    if(dwled->ui.time){
        lv_timer_del(dwled->ui.time);
        dwled->ui.time = NULL;
    }

    if(dwled->ui.bg){
        lv_obj_del(dwled->ui.bg);
        dwled->ui.bg = NULL;
    }

    if(dwled->ui.bg_img){
        lv_obj_del(dwled->ui.bg_img);
        dwled->ui.bg_img = NULL;
    }
    if(dwled->ui.btn){
        lv_obj_del(dwled->ui.btn);
        dwled->ui.btn = NULL;
        dwled->ui.btn_img = NULL;
    }
    if(dwled->ui.bar){
        lv_obj_del(dwled->ui.bar);
        dwled->ui.bar = NULL;
    }
    if(dwled->ui.bar_img){
        lv_obj_del(dwled->ui.bar_img);
        dwled->ui.bar_img = NULL;
    }
    if(dwled->ui.bar_txt){
        lv_obj_del(dwled->ui.bar_txt);
        dwled->ui.bar_txt = NULL;
    }
    if(dwled->ui.bar_w){
        lv_obj_del(dwled->ui.bar_w);
        dwled->ui.bar_w = NULL;
    }
    if(dwled->ui.bar_w_img){
        lv_obj_del(dwled->ui.bar_w_img);
        dwled->ui.bar_w_img = NULL;
    }
    if(dwled->ui.bar_w_txt){
        lv_obj_del(dwled->ui.bar_w_txt);
        dwled->ui.bar_w_txt = NULL;
    }

    return 0;
}


static int ui_dwled_create(lv_obj_t* parent,void* ui_data)
{
    ui_dev_dwled_t* dwled = NULL;
    dwled = (ui_dev_dwled_t*)ui_data;

    lv_obj_t* superview = parent;

    ui_dev_dwled_create(parent,ui_data);
    ui_dev_off_create(superview,&dwled->ui.dev_off);
    ui_dev_off_line_create(superview,&dwled->ui.dev_off_line);

    switch(dwled->data.status){
    case DEV_WORK:
    {
        ui_dev_dwled_show(dwled);
        ui_dev_off_hide(&dwled->ui.dev_off);
        ui_dev_off_line_hide(&dwled->ui.dev_off_line);
        break;
    }
    case DEV_OFF:
    {
        ui_dev_dwled_hide(dwled);
        ui_dev_off_show(&dwled->ui.dev_off);
        ui_dev_off_line_hide(&dwled->ui.dev_off_line);
        break;
    }
    case DEV_OFF_LINE:
    {
        ui_dev_dwled_hide(dwled);
        ui_dev_off_hide(&dwled->ui.dev_off);
        ui_dev_off_line_show(&dwled->ui.dev_off_line);
        break;
    }
    default:break;
    }
 
    return 0;
}

static int ui_dwled_destroy(void* ui_data)
{
    ui_dev_dwled_t* dwled = (ui_dev_dwled_t*)ui_data;

    ui_dev_off_destroy(&dwled->ui.dev_off);
    ui_dev_off_line_destroy(&dwled->ui.dev_off_line);

    ui_dev_dwled_destroy(ui_data);

    return 0;
}

static void dev_dwled_on_custom_attr_config(page_base_t* self)
{


}
static void dev_dwled_on_view_load(page_base_t* self)
{
    // ui_dwled_create(self);
}
static void dev_dwled_on_view_did_load(page_base_t* self)
{
    
}
static void dev_dwled_on_view_will_appear(page_base_t* self)
{

}
static void dev_dwled_on_view_did_appear(page_base_t* self)
{

}
static void dev_dwled_on_view_will_disappear(page_base_t* self)
{

}
static void dev_dwled_on_view_did_disappear(page_base_t* self)
{

}
static void dev_dwled_on_view_did_unload(page_base_t* self)
{
    // ui_dwled_destroy(self);
}

dev_info_t* dev_dwled_reg(ui_dwled_data_t* dev)
{
    int len = 0;
    ui_dev_dwled_t* dwled    = NULL;
    page_vtable_t*  method   = NULL;
    dev_info_t*     devinfo  = NULL;

    devinfo = (dev_info_t*)malloc(sizeof(dev_info_t));
    if(!devinfo){
        debug_d("merr\n");
        return NULL;
    }

    dwled = (ui_dev_dwled_t*)malloc(sizeof(ui_dev_dwled_t));
    if(!dwled){
        debug_d("merr\n");
        return NULL;
    }

    memset(dwled,0,sizeof(ui_dev_dwled_t));

    method = (page_vtable_t*)malloc(sizeof(page_vtable_t));
    if(!method){
        debug_d("merr\n");
        return NULL;
    }
    //***********************************************************
    method->on_custom_attr_config  = dev_dwled_on_custom_attr_config;
    method->on_view_load           = dev_dwled_on_view_load;
    method->on_view_did_load       = dev_dwled_on_view_did_load;
    method->on_view_will_appear    = dev_dwled_on_view_will_appear;
    method->on_view_did_appear     = dev_dwled_on_view_did_appear;
    method->on_view_will_disappear = dev_dwled_on_view_will_disappear;
    method->on_view_did_disappear  = dev_dwled_on_view_did_disappear;
    method->on_view_did_unload     = dev_dwled_on_view_did_unload;
    //***********************************************************
    memcpy(&dwled->data,dev,sizeof(ui_dwled_data_t));
    //***********************************************************
    devinfo->method     = method;    
    devinfo->priv_data  = (void*)dwled;
    //***********************************************************

    //***********************************************************

    return devinfo;
}

int dev_dwled_unreg(dev_info_t* devinfo)
{
    


    return 0;
}

static void dev_dwled_on_page_create(lv_obj_t* parent,void* ui_data)
{
    debug_d("create\n");
    ui_dwled_create(parent,ui_data);
}
static void dev_dwled_on_page_destroy(void* ui_data)
{
    debug_d("destroy\n");
    ui_dwled_destroy(ui_data);
}
static void dev_dwled_on_page_update(void* ui_data)
{

}
page_info_t* dev_dwled_page_reg(ui_dev_dwled_t* dwled)
{
    page_method_t*  method  = NULL;
    page_info_t*    page    = NULL;

    page = (page_info_t*)malloc(sizeof(page_info_t));
    if(!page){
        debug_e("err0\n");
        return NULL;
    }
    method = (page_method_t*)malloc(sizeof(page_method_t));
    if(!method){
        debug_e("err1\n");
        return NULL;
    }
    //***********************************************************
    method->on_page_create  = dev_dwled_on_page_create;
    method->on_page_destroy = dev_dwled_on_page_destroy;
    method->on_page_update  = dev_dwled_on_page_update;
    //***********************************************************
    page->method  = method;    
    page->data    = (void*)dwled;
    //***********************************************************

    return page;
}
